const router = require('koa-router')();
const query = require('../db/my');

router.post('/', async (ctx) => {
  const { type } = ctx.request.body;
  let type1 = JSON.stringify(type);
  // const offset = (page - 1) * pageSize
  console.log(type1, 'type');
  if (type === '出差') {
    let res1 = await query(`select * from chuchai`);
    ctx.body = {
      code: 200,
      msg: '成功',
      data: res1,
    };
  }
  if (type === '借款') {
    let res1 = await query(`select * from jiekuan`);
    ctx.body = {
      code: 200,
      msg: '成功',
      data: res1,
    };
  }
  if (type === '工作请示') {
    let res1 = await query(`select * from qingshi`);
    ctx.body = {
      code: 200,
      msg: '成功',
      data: res1,
    };
  }
});
// 请假申请
router.get('/oneTodo', async (ctx) => {
  // let res1 = await query(`select * from login`);
  ctx.body = {
    code: 200,
    msg: '成功',
    data: 111,
  };
});
router.post('/del', async (ctx) => {
  const { key, type } = ctx.request.body;
  console.log(key, '------key');
  if (type === '出差申请') {
    let res1 = await query(`delete from chuchai where id=${key}`);
    ctx.body = {
      code: 200,
      msg: '删除成功',
    };
  }
  if (type === '借款申请') {
    let res1 = await query(`delete from jiekuan where id=${key}`);
    ctx.body = {
      code: 200,
      msg: '删除成功',
    };
  }
  if (type === '工作请示') {
    let res1 = await query(`delete from qingshi where id=${key}`);
    ctx.body = {
      code: 200,
      msg: '删除成功',
    };
  }
  let res1 = await query(`delete from qing where id=${key}`);
  ctx.body = {
    code: 200,
    msg: '删除成功',
  };
});
// 请假申请中的详情页
router.post('/dailte', async (ctx) => {
  const { id, type } = ctx.request.body;
  console.log('id', id);
  if (type === '请假申请') {
    let res = await query(`select * from One where id=${id}`);
    ctx.body = {
      code: 200,
      msg: '查找成功',
      data: res,
    };
  }
  if (type === '出差申请') {
    let res = await query(`select * from chuchai where id=${id}`);
    ctx.body = {
      code: 200,
      msg: '查找成功',
      data: res,
    };
  }
  if (type === '借款申请') {
    let res = await query(`select * from jiekuan where id=${id}`);
    ctx.body = {
      code: 200,
      msg: '查找成功',
      data: res,
    };
  }
  if (type === '工作请示') {
    let res = await query(`select * from qingshi where id=${id}`);
    ctx.body = {
      code: 200,
      msg: '查找成功',
      data: res,
    };
  }
  if (type === '客户拜访') {
    let res = await query(`select * from baifang where id=${id}`);
    ctx.body = {
      code: 200,
      msg: '查找成功',
      data: res,
    };
  }
});
// 导出router

//如何进行登录
router.post('/login',async(ctx)=>{
  const res = await query(`select * from login where name='${ctx.request.body.username}' and pass=${ctx.request.body.password}`)
  console.log(res)
  if(res.length === 0){
    ctx.body={
      code:200,
      msg:'用户或密码错误',
    }
  }else{
    ctx.body={
      code:200,
      msg:'登录成功',
      data:res[0]
    }
  }
})



module.exports = router;
